var ptCollectionsValues = {
	remove:function(id,_this){
		$.get("/api/collections/removeAttrValue?pid="+id,function(res){
				
			if(res.error){
				showStickyErrorToast(res.message);
			}else{
				
			}
			
		}).error(function(data, status, headers, config) {
			if(status=="error") showStickyErrorToast(data.responseText);
			console.log(data);
			console.log(status);
			
			
		});
		$(_this).parent().parent().remove();
	},
	getValuesCollection:function(){
		if(!ptCollectionsAttr.change) return false;
		$(".imageLoadingAttributes").show();
		$(".poolAttributes").hide();
		$.get("/api/collections/getAttr?pid="+ptCollections.id,function(res){
			
			if(res.error){
			
			}else{
				$(".formaddcol_value").html( ptCollectionsValues.rawForm(res.data) );				
			}
			$(".imageLoadingAttributes").hide();
			ptCollectionsAttr.change = false;
		}).error(function(data, status, headers, config) {
			if(status=="error") showStickyErrorToast(data.responseText);
			console.log(data);
			console.log(status);
			$(".imageLoadingAttributes").hide();
			
		});
	},
	get:function(){
		$.get("/api/collections/getAttrValue?pid="+ptCollections.id,function(res){
			
			if(res.error){
			
			}else{
				$("#pool_collection_value tbody").html( ptCollectionsValues.poolData(res.data) );
			}
			
		}).error(function(data, status, headers, config) {
			if(status=="error") showStickyErrorToast(data.responseText);
			console.log(data);
			console.log(status);
			$(".imageLoadingAttributes").hide();
			
		});
	},
	rawForm:function(d){
		
		if(d.length == 0){
			return "";
		}else{
			var html = "<form id='formValueCol' >";
			$.each(d,function(x,y){
				
				html+=ptCollectionsValues.rawInput(y);
			
			});
			html+= '<br><a href="javascript:void(0)"  class="button simple  close-modal add-collection-value" onclick=ptCollectionsValues.save()> Save </a><br><br>';
			html+= '</form>';
			return html;
				
			
		}
	},
	rawInput:function(d){
		
		return '<label>'+d.attr.toUpperCase()+'</label><input type="text" placeholder="Enter type '+d.attr+'" name="'+d.attr+'" style="line-height: 10px;">';
	},
	save:function(){
		var dt = $("#formValueCol").serialize();
		$.post("/api/collections/saveValue?pid="+ptCollections.id,dt,function(res){
			if(res.error){
				showStickyErrorToast(res.message);
			}else{
				var pt_col1 = $("#pool_collection_value tbody");
				pt_col1.append( ptCollectionsValues.rows(res.data) );
			}
		}).error(function(data, status, headers, config) {
			if(status=="error") showStickyErrorToast(data.responseText);
			console.log(data);
			console.log(status);
		});
	},
	rows:function(r){
		var y = r;
		var html='<tr class="layout_row">'; 
		html+='<td width="30%">'+y.content+'</td>';
			
		html+='<td>'+y.created+'</td>';
		html+='<td><a href="javascript:void(0)" class="cblack font11"  onclick=ptCollectionsValues.remove("'+y.id+'",this)><i class="fa fa-trash-o"></i></a></td>';
			
			
		html+='</tr>';
		return html;
	},
	poolData:function(d){
			
		if(d.length == 0){
			return "";
		}else{
			var html = "";
			$.each(d,function(x,y){
				
				html+=ptCollectionsValues.rows(y);
			
			});
			return html;
				
			
		}
	}
	
};
